import axios from 'axios'

import nprogress from 'nprogress'
//引入进度条的样式
import 'nprogress/nprogress.css'

import store from '../store'
import { Message } from 'element-ui';

const requests = axios.create({
  //就是基础路径
  baseURL: '/api',
  timeout: 5000,
})

requests.interceptors.request.use((config) => {

  // if (store.state.detail.uuid_token) {
  //   // 给请求头加一个字段
  //   //config: 配置对象，对象里面有一个属性很重要 headers请求头
  //   config.headers.userTempId = store.state.detail.uuid_token;
  // }
  //判断token是否携带
  if (store.state.user.token) {
    config.headers.Authorization = store.state.user.token
  }
  
  //进度条开始
  nprogress.start()
  return config
})

//响应拦截器
requests.interceptors.response.use(
  (res) => {
    // console.log(res, 'request-------------')
    if (res.data.code === 200) {
      //进度条结束
      nprogress.done()
      return res.data
    } else {
      Message({
        type: 'error',
        message: res.data.message
      })
      //进度条结束
      nprogress.done()
      return Promise.reject(new Error('Error'))
    }
  },
  (error) => {
    //进度条结束
    nprogress.done()
    return Promise.reject(new Error('Error'))
  }
)

export default requests
